package jdbc.preparedstatement_;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Scanner;

//演示PreparedStatement使用 dml语句
public class PreparedStatementDML_ {
    public static void main(String[] args) throws IOException, SQLException {
        Scanner scanner = new Scanner(System.in);
        //让用户输入管理员名和密码
        System.out.print("请输入管理员的名字: ");
        String admin_name = scanner.nextLine();
        System.out.print("请输入管理员的新密码: ");
        String admin_pwd = scanner.nextLine();

        //连接数据库
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        Connection connection = DriverManager.getConnection(url, user, password);

        //组织SqL
        //添加记录
        String sql = "insert into admin values (?,?)";
        //String sql = "update admin set pwd = ? where name = ?";
        //String sql = "delete from  admin where name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //赋值
        preparedStatement.setString(1,admin_name);
        preparedStatement.setString(2,admin_pwd);
        //preparedStatement.setString(2, admin_name);
        //执行 dml 语句使用  executeUpdate
        int row = preparedStatement.executeUpdate();
        System.out.println(row > 0 ? "执行成功":"执行失败");
        //关闭连接
        preparedStatement.close();
        connection.close();
    }
}
